package alf.mapping.view.activity;

import alf.common.domain.DomainException;
import alf.mapping.dto.help.Category;
import alf.mapping.dto.help.Entry;
import alf.mapping.dto.help.Link;
import alf.mapping.service.help.HelpService;
import alf.mapping.view.Env;
import alf.mapping.view.Util;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.ImageView;

public class HelpActivity extends Activity {
	HelpService service = HelpService.getInstance();
	Entry entry;
	private static final String GROUP_PREFIX  = "group:";
	private static final String IMAGE_RESOURCE_TYPE = "drawable";
	String pack = "alf.mapping.view.activity";
	ListView listaEnlaces,listaGrupo;
	Link[] enlacesGrupo;
	String group = null;
	ImageView image = null;
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
		setContentView(R.layout.help);
		
		listaEnlaces = (ListView) this.findViewById(R.id.help_links);
		listaGrupo = (ListView) this.findViewById(R.id.help_group_list);
		image = (ImageView) this.findViewById(R.id.help_image);
		String subject = getIntent().getStringExtra(Intent.EXTRA_SUBJECT);
		
		show(new Link("",subject));
		listaEnlaces.setOnItemClickListener(new ListView.OnItemClickListener(){
				public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
					seleccionaEnlace(position,entry.getLinks());
				}
			});
		listaGrupo.setOnItemClickListener(new ListView.OnItemClickListener(){
				public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
					seleccionaEnlace(position,enlacesGrupo);
				}
			});
		
    }

	private void show(Link k) {
		Entry newEntry = null;
		try {
			newEntry = service.getEntry(k);
		} catch (DomainException e) {
			Env.toast(this, e.getMessage());
			entry = new Entry();
			entry.setTitle("Error loading help");
			entry.setContent(getErrorText(e));
		}
		if (newEntry!=null) {
			entry = newEntry;
			renderEntry();
		} else {
			Env.toast(this,"Help entry not found: "+ k.getRef());
		}
	}
	
	private String getErrorText(Throwable t){
		StringBuffer b = new StringBuffer();
		Throwable ex = t;
		while (ex!=null) {
			b.append(ex.getMessage());
			b.append("\n");
			ex = ex.getCause();
		}
		return b.toString();
	}
	
	private void renderEntry() {
		Util.setText(this,R.id.help_title,entry.getTitle());
		Util.setText(this,R.id.help_content,entry.getContent());
	
		populateLinkList(entry.getLinks(),listaEnlaces);
		
		String newGroup= getCategory(entry,GROUP_PREFIX);
		
		if (newGroup!=null && !newGroup.equals(this.group)){
			this.group = newGroup;
			try {
				Util.setText(this,R.id.help_group_name,newGroup);
				enlacesGrupo = service.getCategoryEntrys(GROUP_PREFIX+newGroup);
				populateLinkList(enlacesGrupo, listaGrupo);
			} catch (DomainException e) {
				Env.toast(this,"Error loading group data: "+group);
			}
		}
		int imageId = 0;
		String name = entry.getImage();
		if (name!=null){
		// imageId = this.getResources().getIdentifier(name,IMAGE_RESOURCE_TYPE,pack);
		 imageId = this.getResources().getIdentifier(name,null,null);
		} 
		image.setImageResource(imageId);
	
	}

	private void populateLinkList(Link[] links,ListView listControl) {
		String[] textos = new String[links.length];
		for (int i = 0;i < textos.length;i++) {
			textos[i] = links[i].getName();
		}
		listControl.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, textos));
	}
	
	private String getCategory(Entry entry, String prefix) {
		String group = null;
		if (entry.getCategorys()!=null){
			Category c  = null;
			for (int i=0; group==null && i<entry.getCategorys().length;i++)	{
				c = entry.getCategorys()[i];
				if (c!=null && c.getName().indexOf(prefix)==0){
					String value = c.getName();
					group = value.substring(prefix.length());
				}
			}
		}
		return group;
	}
	
	private void seleccionaEnlace(int position,Link[] links) {
		Link link = links[position];
		show(link);
	}
}


